package cn.gxa.j229.bug.finance.controller.borrowing;

import cn.gxa.j229.bug.finance.BusinessException;
import cn.gxa.j229.bug.finance.pojo.Category;
import cn.gxa.j229.bug.finance.service.borrowing.CategoryService;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 借款标类别接口
 * @Author FanJiang
 * @Date 2020/9/22
 * @Version 1.0
 */

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Resource
    CategoryService categoryService;

    /**
     * 借款标类型
     * 查询所有，页面请求展示
     * @return
     */
    @PostMapping("/findAll")
    @ResponseBody
    public Map findAllAndOrder(){
        List allAndOrder = categoryService.findAllAndOrder();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(allAndOrder);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");

        return jsonResult.getValues();
    }

    /**
     * 标的上架维护
     * 标的类型下拉列表
     * @return
     */
    @PostMapping("/findAllList")
    @ResponseBody
    public Map findAllList(){
        List list = categoryService.findAllList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(list);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");

        return jsonResult.getValues();
    }

    /**
     * 借款标类别
     * 添加分类
     * @param category
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    public Map insertSelective (@RequestBody Category category) throws BusinessException {
        if (category == null || category.equals("")){
            throw new BusinessException("50006","不能插入全为空的数据");
        }
        int insertRow = categoryService.insertSelective(category);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData("插入成功" + insertRow + "行");
        jsonResult.setMsg("成功");
        jsonResult.setCode("200");

        return jsonResult.getValues();
    }

    /**
     * 借款标类别
     * 修改按钮接口
     * @param category
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public Map udateByIdSelective(@RequestBody Category category){
        int updateRow = categoryService.updateByIdSelective(category);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData("更新成功" + updateRow + "行");
        jsonResult.setMsg("成功");
        jsonResult.setCode("200");

        return jsonResult.getValues();
    }

    /**
     * 借款标类别
     * 按照id号删除  /单个删除接口
     * @param category
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Map deleteById(@RequestBody Category category){
        int deleteRow = categoryService.deleteById(category.getId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData("删除成功" + deleteRow + "行");
        jsonResult.setMsg("成功");
        jsonResult.setCode("200");

        return jsonResult.getValues();
    }

    /**
     * 借款标类别
     * 一键更新状态（禁用/有效）接口
     * @param category
     * @return
     */
    @PostMapping("/forbidden")
    @ResponseBody
    public Map updateByIdState(@RequestBody Category category){
        int updateRow = categoryService.updateByIdState(category);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData("成功" + updateRow + "个");
        jsonResult.setMsg("成功");
        jsonResult.setCode("200");

        return jsonResult.getValues();
    }

}
